<?php
class UserLibrary {

	public function uploadCart() {
	}

	public function orderInsert($Check) {
		$CI =& get_instance();

		$buyerPhone = mysql_real_escape_string( $CI->input->post('buyerPhoneNumber01', true)).'-'.
				mysql_real_escape_string($CI->input->post('buyerPhoneNumber02', true)).'-'.
				mysql_real_escape_string($CI->input->post('buyerPhoneNumber03', true));
		//orders table 저장 데이터
		$data = array('order_code' => mysql_real_escape_string($CI->input->post('orderCode', true)),
				'order_password' => md5(mysql_real_escape_string($CI->input->post('orderPassword', true))),
				'buyer_name' => mysql_real_escape_string($CI->input->post('buyerName', true)),
				'buyer_phone' => $buyerPhone,
				'buyer_address' => mysql_real_escape_string($CI->input->post('buyerAddress', true)),
				'total_price' => mysql_real_escape_string($CI->cart->total()),
				'order_date' => mysql_real_escape_string($CI->input->post('orderDate', true)),
				'billing_select' => mysql_real_escape_string($CI->input->post('billingSelect', true)),
				'billing_check' => $Check);

		$CI->db->insert('orders', $data); //orders table insert

		foreach($CI->cart->contents() as $items): //상품별로 저장
		if($Check == 'yes') { //결제확인 'yes'일시
			//order_items table 저장 데이터
			$data = array('order_code' => mysql_real_escape_string($CI->input->post('orderCode', true)),
					'item_code' => $items['id'],
					'item_quantity' => $items['qty'],
					'sub_price' => $items['subtotal'],
					'history' => 'ready'); //배송현황을 '배송준비'로 저장
		} else { //결제확인 'no'일시
			//order_items table 저장 데이터
			$data = array('order_code' => mysql_real_escape_string($CI->input->post('orderCode', true)),
					'item_code' => $items['id'],
					'item_quantity' => $items['qty'],
					'sub_price' => $items['subtotal']);
		}

		$CI->db->insert('order_items', $data); //order_items table insert
		endforeach;
		return mysql_real_escape_string($CI->input->post('orderCode', true));
	}
}